﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;
using UniversityManagementSystem.Manager;
using iTextSharp.text;

namespace UniversityManagementSystem.UI
{
    public partial class StudentRegistrationProcess : System.Web.UI.Page
    {
        List<StudentEducation> studentEducation = new List<StudentEducation>();
        private string regNo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(!IsPostBack)
                {
                    Department department = new Department();
                    List<Department> allDepartments = new List<Department>();
                    allDepartments = department.GetAllDepartments();
                    ViewState["Department"] = allDepartments;
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataValueField = "DepartmentName";
                    departmentDropDownList.DataBind();
                    ViewState["studentEducation"] = studentEducation;
                }
            }
            catch(Exception exception)
            {
                messageLabel.Text = "Error loading Page.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void addToListButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (certificateLocationFileUploader.HasFile)
                {
                    string filePath = System.IO.Path.GetExtension(certificateLocationFileUploader.FileName);
                    if (filePath.ToLower() == ".jpg" || filePath.ToLower() == ".bmp" || filePath.ToLower() == ".gif")
                    {
                        certificateLocationFileUploader.SaveAs(
                            @"F:\C#\Practice\UniversityManagementSystem2\uploadedCertificates\" +
                            certificateLocationFileUploader.FileName);
                        messageLabel.Text = "File: " + certificateLocationFileUploader.PostedFile.FileName +
                                            " has been uploaded.";
                        messageLabel.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {
                        messageLabel.Text = "Only image file can be uploaded. Try again please.";
                        messageLabel.ForeColor = System.Drawing.Color.Red;
                    }
                }
                else
                {
                    messageLabel.Text = "Select certificate first.";
                    messageLabel.ForeColor = System.Drawing.Color.Red;
                }
                studentEducation = (List<StudentEducation>)ViewState["studentEducation"];
                StudentEducation aStudentEducation = new StudentEducation();
                aStudentEducation.ExamName = examinationDropDownList.SelectedValue;
                aStudentEducation.GradeLetter = gradeLetterDropdownList.SelectedValue;
                aStudentEducation.CGPA = cgpaTextBox.Text;
                aStudentEducation.Certificate = certificateLocationFileUploader.FileName;
                studentEducation.Add(aStudentEducation);
                ViewState["studentEducation"] = studentEducation;
                studentInformationGridView.AutoGenerateColumns = false;
                List<StudentEducation> allEducations = new List<StudentEducation>();
                allEducations = (List<StudentEducation>)ViewState["studentEducation"];
                studentInformationGridView.DataSource = allEducations;
                studentInformationGridView.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error loading student";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            string saveMessage = "";
            try
            {
                Department aDepartment = new Department();
                List<Department> allDepartment = (List<Department>) ViewState["Department"];
                aDepartment = allDepartment.Find(delegate(Department FindaDepartment) { return FindaDepartment.DepartmentName == departmentDropDownList.SelectedValue; });
                List<StudentEducation> aStudentEducation = new List<StudentEducation>();
                aStudentEducation = (List<StudentEducation>) ViewState["studentEducation"];
                Student aStudent = new Student();
                aStudent.name = studentNameTextBox.Text;
                aStudent.email = emailTextBox.Text;
                aStudent.departmentName = departmentDropDownList.SelectedValue;
                aStudent.contactNo = contactNoTextBox.Text;
                aStudent.registrationDate = registrationDateTextBox.Text;
                aStudent.address = addressTextBox.Text;
                saveMessage = aStudent.SaveAstudent(aStudent, aStudentEducation, aDepartment);
                messageLabel.Text = saveMessage;
                messageLabel.ForeColor = System.Drawing.Color.Blue;
            }
            catch (Exception)
            {
                messageLabel.Text = "Unable to register student";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }
    }
}